﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Aga.Psad.Backend.Entities;
using Aga.Psad.Backend.Repositories;
using Aga.Psad.Mvc.Services.Dtos;
using AutoMapper;

namespace Aga.Psad.Mvc.Services
{
    // NOTE: If you change the class name "Clientes" here, you must also update the reference to "Clientes" in Web.config.
    public class UploadDataService : IUploadDataService
    {

        #region IUploadDataService Members

        public bool CheckService()
        {
            return true;
        }

        public int NuevoDeclarante(ClienteDto dto)
        {
            Mapper.CreateMap<ClienteDto, Cliente>().ForMember(c => c.Id, a => a.Ignore());

            Cliente entity = Mapper.Map<ClienteDto, Cliente>(dto);
            
            ClienteRepository repository = new ClienteRepository();
            repository.Save(entity);

            return entity.Id;
        }

        public int NuevoImportador(ClienteDeClienteDto dto)
        {
            Mapper.CreateMap<ClienteDeClienteDto, ClienteDeCliente>().ForMember(c => c.Id, a => a.Ignore());

            ClienteDeCliente entity = Mapper.Map<ClienteDeClienteDto, ClienteDeCliente>(dto);

            ClienteDeClienteRepository repository = new ClienteDeClienteRepository();
            repository.Save(entity);
            
            return entity.Id;
        }

        public void NuevoDespacho(DespachoDto dto)
        {
            DespachoRepository depachoRepository = new DespachoRepository();

            Mapper.CreateMap<DespachoDto, Despacho>().ForMember(c => c.Id, a => a.Ignore());

            Despacho entity = Mapper.Map<DespachoDto, Despacho>(dto);

            depachoRepository.Save(entity);
        }

        public void ActualizarDespacho(DespachoDto dto)
        {
            DespachoRepository depachoRepository = new DespachoRepository();

            Despacho entity = depachoRepository.GetById(dto.Id);

            Mapper.CreateMap<DespachoDto, Despacho>().ForMember(c => c.Id, a => a.Ignore());
            
            entity = Mapper.Map<DespachoDto, Despacho>(dto, entity);

            depachoRepository.Save(entity);
        }

        public DespachoDto ObtenerDespachoPorLegajo(string legajo)
        {
            DespachoRepository repository = new DespachoRepository();
            var depacho = repository.GetByLegajo(legajo);

            Mapper.CreateMap<Despacho, DespachoDto>();

            var dto = Mapper.Map<Despacho, DespachoDto>(depacho);

            return dto;
        }

        public ClienteDto ObtenerDeclarantePorExId(int exId)
        {
            ClienteRepository repository = new ClienteRepository();
            var declarante = repository.GetByExId(exId);

            Mapper.CreateMap<Cliente, ClienteDto>();

            var dto = Mapper.Map<Cliente, ClienteDto>(declarante);

            return dto;
        }

        public ClienteDeClienteDto ObtenerImportadorPorExId(int exId)
        {
            ClienteDeClienteRepository repository = new ClienteDeClienteRepository();
            var importador = repository.GetByExId(exId);

            Mapper.CreateMap<ClienteDeCliente, ClienteDeClienteDto>();

            var dto = Mapper.Map<ClienteDeCliente, ClienteDeClienteDto>(importador);

            return dto;
        }

        public ClienteDeClienteDto ObtenerImportadorPorCuit(int clienteExId, string cuitImprotador)
        {
            ClienteRepository repository = new ClienteRepository();
            var declarante = repository.GetByExId(clienteExId);

            if (declarante == null)
                return null;

            declarante.Clientes.Load();

            var importador = declarante.Clientes.Where(c => c.Cuit.Equals(cuitImprotador)).FirstOrDefault();

            Mapper.CreateMap<ClienteDeCliente, ClienteDeClienteDto>();

            var dto = Mapper.Map<ClienteDeCliente, ClienteDeClienteDto>(importador);

            return dto;
        }

        public ClienteDto[] ObtenerTodosLosDeclarantes()
        {
            ClienteRepository repository = new ClienteRepository();
            var declarantes = repository.GetAll();

            Mapper.CreateMap<Cliente, ClienteDto>();

            var dtos = Mapper.Map<List<Cliente>, ClienteDto[]>(declarantes);

            return dtos;
        }

        public ClienteDeClienteDto[] ObtenerTodosLosImportadores()
        {
            ClienteDeClienteRepository repository = new ClienteDeClienteRepository();
            
            var importadores = repository.GetAll();

            Mapper.CreateMap<ClienteDeCliente, ClienteDeClienteDto>();

            var dtos = Mapper.Map<List<ClienteDeCliente>, ClienteDeClienteDto[]>(importadores);

            return dtos;
        }

        public ClienteDeClienteDto[] ObtenerImportadoresPorClienteId(int clienteId)
        {
            ClienteDeClienteRepository repository = new ClienteDeClienteRepository();

            var importadores = repository.DC.ClienteDeClientes
                                            .Where(c => c.ClienteId == clienteId)
                                            .ToList();

            Mapper.CreateMap<ClienteDeCliente, ClienteDeClienteDto>();

            var dtos = Mapper.Map<List<ClienteDeCliente>, ClienteDeClienteDto[]>(importadores);

            return dtos;
        }

        #endregion
    }
}
